﻿using System;
using System.Collections.Generic;
using System.Data;

using CoreLab.Oracle; //using Devart.Data.Oracle; //using Oracle.DataAccess.Client;
//using Oracle.DataAccess.Types;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Oracle;

namespace DBQuery
{
    /// <summary>
    /// Summary description for FeedGeneratorTable
    /// </summary>
    public static class FeedGeneratorTable
    {

        public static int SaveFeedGenerator(int iFeedSourceId, String sxSQL, String sxDesc, String sxConnection, int iInterval)
        {
            return SaveFeedGenerator(new DBLib(), iFeedSourceId, sxSQL, sxDesc, sxConnection, iInterval);
        }
        public static int SaveFeedGenerator(DBLib dbl, int iFeedSourceId, String sxSQL, String sxDesc, String sxConnection, int iInterval)
        {
            List<OracleParameter> lsParams = new List<OracleParameter>();

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "out_new_id";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Direction = ParameterDirection.Output;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_FEED_SOURCE_ID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Value = iFeedSourceId;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_SQL";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            lsParams[lsParams.Count - 1].Value = sxSQL;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_DESCRIPTION";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            lsParams[lsParams.Count - 1].Value = sxDesc;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_CONNECTION";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            lsParams[lsParams.Count - 1].Value = sxConnection;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_MINUTE_INTERVAL";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Value = iInterval;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_CREATE_BY";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            lsParams[lsParams.Count - 1].Value = ICPUser.ID;

            String sxQuery = "PR_INSERT_FEED_GENERATOR";

            dbl.ExecuteProcedure(sxQuery, lsParams.ToArray());
            
            return Convert.ToInt32(lsParams[0].Value);
        }

        public static void UpdateFeedGenerator(int iFeedGeneratorId, String sxSQL, String sxDesc, String sxConnection, int iInterval)
        {
            UpdateFeedGenerator(new DBLib(), iFeedGeneratorId, sxSQL, sxDesc, sxConnection, iInterval);
        }
        public static void UpdateFeedGenerator(DBLib dbl, int iFeedGeneratorId, String sxSQL, String sxDesc, String sxConnection, int iInterval)
        { 
            List<OracleParameter> lsParams = new List<OracleParameter>();

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_FEED_GENERATOR_ID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Value = iFeedGeneratorId;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_SQL";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            lsParams[lsParams.Count - 1].Value = sxSQL;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_DESCRIPTION";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            lsParams[lsParams.Count - 1].Value = sxDesc;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_CONNECTION";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            lsParams[lsParams.Count - 1].Value = sxConnection;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_MINUTE_INTERVAL";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Value = iInterval;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_UPDATE_BY";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            lsParams[lsParams.Count - 1].Value = ICPUser.ID;

            String sxQuery = "PR_UPDATE_FEED_GENERATOR";

            dbl.ExecuteProcedure(sxQuery, lsParams.ToArray());
            
        }

        public static void DeleteFeedGenerator(int iFeedGeneratorId)
        {
            DeleteFeedGenerator(new DBLib(), iFeedGeneratorId);
        }
        public static void DeleteFeedGenerator(DBLib dbl, int iFeedGeneratorId)
        {
            List<OracleParameter> lsParams = new List<OracleParameter>();

            OracleParameter param = new OracleParameter();
            param.ParameterName = "prmID";
            param.OracleDbType = OracleDbType.Number;
            param.Value = iFeedGeneratorId;

            String sxQuery = " delete from FEED_GENERATOR where FEED_GENERATOR_ID = :prmID";

            dbl.ExecuteNonQuery(sxQuery, param);
            
        }

        public static void ToggleFeedGenerator(int iFeedGeneratorId)
        {
            ToggleFeedGenerator(new DBLib(), iFeedGeneratorId);
        }
        public static void ToggleFeedGenerator(DBLib dbl, int iFeedGeneratorId)
        {
            List<OracleParameter> lsParams = new List<OracleParameter>();

            OracleParameter param = new OracleParameter();
            param.ParameterName = "IN_FEED_GENERATOR_ID";
            param.OracleDbType = OracleDbType.Number;
            param.Value = iFeedGeneratorId;

            String sxQuery = "PR_TOGGLE_FEED_GENERATOR";

            dbl.ExecuteProcedure(sxQuery, param);
            
        }
    }
}